﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Windows.Forms;

namespace Skipper_Connector
{
    class XMLHelper
    {
        private string xmlName;
        /*
         * 初始化xml配置文件
         */
        public XMLHelper(string XMLName)
        {
            xmlName = XMLName;
            if (!File.Exists(xmlName))
            {
                XDocument xmldoc = new XDocument(
                    new XElement("Configuration",
                        new XElement("BasicSet", new XAttribute("trsPort", ""),
                            new XAttribute("recPort", ""),
                            new XAttribute("machinePath", ""),
                            new XAttribute("codePath", ""),
                            new XAttribute("PortState", false),
                            new XAttribute("modeState", 1),
                            new XAttribute("toolfile",""),
                            new XAttribute("NPAN","0"),
                            new XAttribute("OutputCsv",""),
                            new XAttribute("PMOD","0"),
                            new XAttribute("IsMachiningTransform",true),
                            new XAttribute("FileExtension",".bpp"),
                            new XAttribute("CurrentToolFile",""),
                            new XAttribute("LastToolFile","")
                                        )
                                    )
                                                    );
                xmldoc.Save(xmlName);
            }
        }
        /*
         *设置属性值
         */
        public void setXmlValue(string xmlElement, string xmlAttribute, string xmlValue)
        {
            XDocument xmldoc = XDocument.Load(xmlName);
            xmldoc.Element("Configuration").Element(xmlElement).Attribute(xmlAttribute).Value = xmlValue;
            xmldoc.Save(xmlName);
        }
        /*
         *获取属性值 
         */
        public string getXmlValue(string xmlElement, string xmlAttribute)
        {
            XDocument xmldoc = XDocument.Load(xmlName);
            var results = from c in xmldoc.Descendants(xmlElement)
                          select c;
            string s = "";
            foreach (var result in results)
            {
                s = result.Attribute(xmlAttribute).Value.ToString();
            }
            return s;
        }
        /*
        * 添加新元素及属性组
        */
        public void ElementAdd(string ParaentElement, string xmlElement, string[,] xmlAttributes)
        {
            XDocument rootNodes = XDocument.Load(xmlName);
            XElement childNode = new XElement(xmlElement);
            for (int i = 0; i < xmlAttributes.Length / 2; i++)
            {
                XAttribute myAttribute = new XAttribute(xmlAttributes[i, 0].ToString(), xmlAttributes[i, 1]);
                childNode.Add(myAttribute);
            }
            var elemnt = from c in rootNodes.Descendants(ParaentElement)
                         select c;
            foreach (var s in elemnt)
            {
                s.Add(childNode);
            }
            rootNodes.Save(xmlName);
        }
        /*
         * 添加新元素及属性
         */
        public void ElementAdd(string ParaentElement, string xmlElement, string xmlAttriubtes, string value)
        {
            XDocument rootNodes = XDocument.Load(xmlName);
            XElement childNode = new XElement(xmlElement);
            XAttribute myAttribute = new XAttribute(xmlAttriubtes, value);
            childNode.Add(myAttribute);
            var elemnt = from c in rootNodes.Descendants(ParaentElement)
                         select c;
            foreach (var s in elemnt)
            {
                s.Add(childNode);
            }
            rootNodes.Save(xmlName);
        }
        /*
         * 添加新元素
         */
        public void ElementAdd(string ParaentElement, string xmlElement)
        {
            XDocument rootNodes = XDocument.Load(xmlName);
            XElement childNode = new XElement(xmlElement);
            var elemnt = from c in rootNodes.Descendants(ParaentElement)
                         select c;
            foreach (var s in elemnt)
            {
                s.Add(childNode);
            }
            rootNodes.Save(xmlName);
        }
        /*
         * 添加新属性
         */
        public void AttributeAdd(string xmlElement, string Attri, string value)
        {
            XElement myElement = XElement.Load(xmlName);
            XAttribute myattr = new XAttribute(Attri, value);
            var s = from c in myElement.Descendants(xmlElement)
                    select c;
            foreach (var d in s)
            {
                d.Add(myattr);
            }
            myElement.Save(xmlName);
        }
    }
}

